﻿// JESUS IS LORD! ONLY THROUGH CHRIST JESUS IS THIS POSSIBLE  JESUS IS LORD!
// JESUS IS LORD! REV JIMMY ELLIS. RESUME WEBSITE: WWW.JIMMYEAPLUS.COM  | BIBLE API/XML WEBSITE: WWW.BIBLEXML.COM JESUS IS LORD!
/* JESUS IS LORD! THIS IS A THINKGEAR NET REHASH FILESTREAM CLASS USAGE. YOU SEE THINKGEAR NET ONLY WORKS WITH .NET 3.5 AND NOT 4.0 AS SUCH IS INCOMPATIBLE WITH ALL .NET 4.0 APPLICATIONS JESUS IS LORD!
 * JESUS IS LORD! SO WHAT THIS IS A .NET 3.5 APPLICATION THAT REROUTES ALL DATA COLLECTED FROM THE HEADSET INTO A UNIVERSALLY ACCEPTABLE FORMAT (I.E. TEXT FORMAT) AND SENDS IT TO THE .NET 4.0 APPLICATION JESUS IS LORD!
 * JESUS IS LORD! THINK GEAR WRAPPER HAS BEEN EDITED AND SO HAS THE WHOLE APPLICATION TO DISPLAY NORMALLY BUT ALSO FILESTREAM TO A TEXTFILE DIRECTORY. JESUS IS LORD!
 * JESUS IS LORD! THE APP INVENTOR/CODER/PROGRAMMERS JOB IS TWO FOLD 1. EDIT THE METHOD "sendmeditationdata" SO THAT IT WRITES THE THE TEXT FILE WITHIN THEIR APPLICATION AND CREATE TEXT OF THAT NAME. 2. USE THE OTHER CUSTOM  JESUS IS LORD!
 * JESUS IS LORD! METHOD CONTAINED IN THE READ ME CODE AND BELOW THIS APPLICATION IN COMMENT FORM IN THEIR .NET 4.0 (TESTED ONLY IN 4.0) TO RECEIVE THE DATA IN REAL TIME. THIS EFFECTIVELY ADDS ANOTHER LAYER OF UNRELIABILITY JESUS IS LORD!
 * JESUS IS LORD! BECAUSE IF THE FILE STREAM IS NOT EXECUTED WITH THE CORRECT INSTRUCTIONS OR WITH THE CORRECT FILE OR THE TEXT FILE IS OPEN OR IF THE FILESTREAM READS WHILE IT IS STILL WRITING IT WILL NOT WORK. DONOT CHANGE ANY OF THE SETTINGS JESUS IS LORD!
 * JESUS IS LORD! VERSION 2.1 WILL HAVE FILESTREAM ERROR EXCEPTIONS TO FIX THIS AND ACCEPT MORE THAN JUST MEDITATION DATA (NOT ATTENTION DATE AS OF YET), AS WELL AS A PROPER SAMPLE APPLICATION INTERFACED .NET 3.5 TO .NET 4.0 BUT THAT IS NOT WHAT WE HAVE ADDED HERE WE HAVE ONLY VERSION 2.0 RIGHT NOW SO LET'S ALL BE HAPPY WE CAN USE THE HEADSET IN OUR .NET 4.0 APPLICATIONS JESUS IS LORD!
*/
using System;
using System.IO.Ports;
using System.IO;
using System.Threading;
using System.Windows.Forms;
using System.Xml;
using ThinkGearNET;
using System.Web;
using System.Net;
using System.Text;

namespace ThinkGearNETTest
{
    public partial class Form1 : Form
    {
        
        public float meditationvar;
        public float attentionvar;
        public string meditationstring;
        public string attentionstring;

        private ThinkGearWrapper _thinkGearWrapper = new ThinkGearWrapper();

        public Form1()
        {
            InitializeComponent();
     
        }

        public void Form1_Load(object sender, EventArgs e)
        {
            foreach (string port in SerialPort.GetPortNames())
                cboPort.Items.Add(port);
            cboPort.SelectedIndex = 0;
        }

        public void btnConnect_Click(object sender, EventArgs e)
        {
            _thinkGearWrapper = new ThinkGearWrapper();

            // setup the event
            _thinkGearWrapper.ThinkGearChanged += _thinkGearWrapper_ThinkGearChanged;

            // connect to the device on the specified COM port at 57600 baud
            if (!_thinkGearWrapper.Connect(cboPort.SelectedItem.ToString(), 57600, true))
                MessageBox.Show("Could not connect to headset.");
        }

        public void _thinkGearWrapper_ThinkGearChanged(object sender, ThinkGearChangedEventArgs e)
        {
            // update the textbox and sleep for a tiny bit
            BeginInvoke(new MethodInvoker(delegate
            {
                lblAttention.Text = "Attention: " + e.ThinkGearState.Attention;
                lblMeditation.Text = "Meditation: " + e.ThinkGearState.Meditation;
                attentionvar = e.ThinkGearState.Attention;
                meditationvar = e.ThinkGearState.Meditation;
                attentionstring = attentionvar.ToString();
                meditationstring = meditationvar.ToString();
                txtState.Text = e.ThinkGearState.ToString();
            }));
            Thread.Sleep(10);
            //JESUS IS GOD!


            // THINKGEAR .NET 2.0 CODE 
            // THINKGEAR .NET 2.0 CODE 
            // THINKGEAR .NET 2.0 CODE 
            #region THINKGEAR NET 2.0 CODE
            senddatameditation();
            #endregion 
            // THINKGEAR .NET 2.0 CODE 
            // THINKGEAR .NET 2.0 CODE 
            // THINKGEAR .NET 2.0 CODE 

        }

  

        #region THINKGEAR NET 2.0 CODE
        public void senddatameditation()
        {
            // THINKGEAR .NET 2.0 CODE 
            // THINKGEAR .NET 2.0 CODE 
            // THINKGEAR .NET 2.0 CODE 
            // THINKGEAR .NET 2.0 CODE 
            // JESUS IS LORD <<< FILE WRITING >>> YOU MUST CREATE A PATH FOR A TEXT FILE TO SEND ALL HEADSET DATA 
            // JESUS IS LORD <<< FILE WRITING >>> YOU MUST CREATE A PATH FOR A TEXT FILE TO SEND ALL HEADSET DATA 
            // JESUS IS LORD <<< FILE WRITING >>> YOU MUST CREATE A PATH FOR A TEXT FILE TO SEND ALL HEADSET DATA 
            // JESUS IS LORD <<< FILE WRITING >>> YOU MUST CREATE A PATH FOR A TEXT FILE TO SEND ALL HEADSET DATA 
            FileStream fs = new FileStream("insertpathofemptytextfilehere", FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite);
            fs.Close();
            // JESUS IS LORD <<< FILE WRITING >>> YOU MUST CREATE A PATH FOR A TEXT FILE TO SEND ALL HEADSET DATA 
            // JESUS IS LORD <<< FILE WRITING >>> YOU MUST CREATE A PATH FOR A TEXT FILE TO SEND ALL HEADSET DATA 
            // JESUS IS LORD <<< FILE WRITING >>> YOU MUST CREATE A PATH FOR A TEXT FILE TO SEND ALL HEADSET DATA 
            // JESUS IS LORD <<< FILE WRITING >>> YOU MUST CREATE A PATH FOR A TEXT FILE TO SEND ALL HEADSET DATA 
            // JESUS IS LORD <<< FILE WRITING >>> YOU MUST CREATE A PATH FOR A TEXT FILE TO SEND ALL HEADSET DATA 
            // JESUS IS LORD <<< FILE WRITING >>> YOU MUST CREATE A PATH FOR A TEXT FILE TO SEND ALL HEADSET DATA 
            StreamWriter sw = new StreamWriter("insertpathofemptytextfilehere", true, Encoding.ASCII);
            string nextline = meditationstring;
            
            sw.WriteLine(nextline);
            sw.Close();
            // THINKGEAR .NET 2.0 CODE 
            // THINKGEAR .NET 2.0 CODE 
            // THINKGEAR .NET 2.0 CODE 
            // THINKGEAR .NET 2.0 CODE 
            // THINKGEAR .NET 2.0 CODE 
        }
        #endregion 



        public void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            _thinkGearWrapper.Disconnect();
        }

        public void btnDisconnect_Click(object sender, EventArgs e)
        {
            _thinkGearWrapper.Disconnect();
        }

        public void btnEnableBlink_Click(object sender, EventArgs e)
        {
            _thinkGearWrapper.EnableBlinkDetection(true);
        }
        
        public void btnDisableBlink_Click(object sender, EventArgs e)
        {
            _thinkGearWrapper.EnableBlinkDetection(false);
        }

        public void txtState_TextChanged(object sender, EventArgs e)
        {

        }

        // THINKGEAR .NET 2.0 CODE 
        // THINKGEAR .NET 2.0 CODE 
        // THINKGEAR .NET 2.0 CODE 
        // THINKGEAR .NET 2.0 CODE 
        // THINKGEAR .NET 2.0 CODE 
        #region THINKGEAR NET 2.0 CODE
        public class minddata
        {
            //public String title;
            public String meditation;
            // public String attention;
        }
        #endregion 

        // THINKGEAR .NET 2.0 CODE 
        // THINKGEAR .NET 2.0 CODE 
        // THINKGEAR .NET 2.0 CODE 
        // THINKGEAR .NET 2.0 CODE 
        // THINKGEAR .NET 2.0 CODE 
    }
}

// THINKGEAR .NET 2.0 CODE 
// THINKGEAR .NET 2.0 CODE 
// THINKGEAR .NET 2.0 CODE 
// THINKGEAR .NET 2.0 CODE 
// THINKGEAR .NET 2.0 CODE 
#region THINKGEAR NET 2.0 CODE
/*
 * 
 *         public string minddatareceive(string acceptthedata)
        {
            
            
                FileStream fs = new FileStream("insertfilepathfortextfilehere", FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
                // fs.Close();
                StreamReader reader = new StreamReader(fs);
                acceptthedata = reader.ReadLine();

                if ((acceptthedata == null) || (acceptthedata == "") )
                {
                    acceptthedata = "0";
                    return acceptthedata; // acceptthedata;
                }
                else
                {
                   
                    return acceptthedata; //acceptthedata;
                } */


/* } */
#endregion

#region 

/*
 * 
  string mindDataStr; 
  int mindData; 
  mindData = Convert.ToInt32(mindDataStr);
 * /
 * 
*/

#endregion 

